package com.lvhx.sofa3.server.filter;

import com.alibaba.fastjson.JSON;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.filter.AutoActive;
import com.alipay.sofa.rpc.filter.Filter;
import com.alipay.sofa.rpc.filter.FilterInvoker;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Extension("testFilter")
// 参数 providerSide 表示是否生效于服务端；参数 consumerSide 表示是否生效于客户端
@AutoActive(providerSide =true, consumerSide =false)
public class TestFilter extends Filter {

    // 判断是否需要走过滤器
    @Override
    public boolean needToLoad(FilterInvoker invoker) {
        return super.needToLoad(invoker);
    }

    @Override
    public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
        log.info("=====RPC请求入参："+ JSON.toJSONString(request.getMethodArgs()));
        SofaResponse response = invoker.invoke(request);
        log.info("=====RPC请求出参："+ JSON.toJSONString(response));
        return response;
    }
}
